package config;

import com.alibaba.druid.pool.DruidDataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("service")
@MapperScan("mapper")
public class SpringConfig {

    //创建数据源
    @Bean
    DataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///gaofeng2207");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        return dataSource;
    }

    //创建会话工厂
    @Bean
    SqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssf=new SqlSessionFactoryBean();
        ssf.setDataSource(dataSource);
        //加载config.xml配置文件
        // 加载config.xml文件
        Resource resource = new ClassPathResource("config.xml");
        ssf.setConfigLocation(resource);
        return ssf;
    }
}
